package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {
    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private RuntimeService runtimeService;
    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(ServiceItem serviceItem) {
        //判断是套餐设置审核状态为初始化
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else{
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        serviceItem.setCreateTime(new Date());
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem oldObj = this.get(serviceItem.getId());
        //上架，不能进行编辑
        if (ServiceItem.SALESTATUS_ON.equals(oldObj.getSaleStatus())){
            throw new BusinessException("服务单项处于上架状态，不允许进行修改");
        }
        //未上架，套餐处于审核中，不能进行编辑
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) &&
              ServiceItem.AUDITSTATUS_AUDITING.equals(oldObj.getAuditStatus())){
            throw new BusinessException("服务套餐处于审核中状态，不允许进行修改");
        }
        //未上架，审核通过了，再重新编辑
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) &&
                ServiceItem.AUDITSTATUS_APPROVED.equals(oldObj.getAuditStatus())){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else{
            //原来的审核状态
            serviceItem.setAuditStatus(oldObj.getAuditStatus());
        }
        //更新修改成下架状态
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] Ids = Convert.toLongArray(ids);
        for (Long id : Ids) {
            serviceItemMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleOff(Long id) {
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_OFF);
    }

    @Override
    public void saleOn(Long id) {
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_ON);
    }

    @Override
    @Transactional
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        //合理性校验
        ServiceItem serviceItem = this.get(id);
        if (ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())//处于上架状态
            || ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage())//非套餐
            || ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus())//审核中
            || ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())//审核通过
            || ServiceItem.AUDITSTATUS_NO_REQUIRED.equals(serviceItem.getAuditStatus())//无需审核
        ){
            throw new BusinessException("非法的操作");
        }
        CarPackageAudit audit = new CarPackageAudit();
        audit.setCreateTime(new Date());
        audit.setAuditorId(director);
        audit.setBpmninfoId(bpmnInfoId);
        audit.setCreator(ShiroUtils.getUser().getUserName());
        audit.setInfo(info);
        audit.setServiceItemId(id);
        audit.setServiceItemName(serviceItem.getName());
        audit.setServiceItemPrice(serviceItem.getDiscountPrice());
        audit.setServiceItemInfo(serviceItem.getInfo());
        //保存对象
        carPackageAuditService.save(audit);
        //启动流程
        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);
        Map<String,Object> params = new HashMap<>();
        params.put("discountPrice",serviceItem.getDiscountPrice().longValue());
        params.put("director",director);
        if (finance!=null){
            params.put("finance",finance);
        }
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(), String.valueOf(audit.getId()), params);
        audit.setInstanceId(processInstance.getId());
        carPackageAuditService.update(audit);
        serviceItemMapper.changeAuditStatus(id,ServiceItem.AUDITSTATUS_AUDITING);
    }

    @Override
    public void changeAuditStatus(Long serviceItemId, Integer status) {
        serviceItemMapper.changeAuditStatus(serviceItemId,status);
    }
}
